﻿namespace Pap.Web.Modules.Loan.Views
{

    using System;
    using Coolite.Ext.Web;
    using global::Utils.UIPermission;
    using Pap.Data;
    using Pap.Web.Modules.Loan.ViewModel;
    using Pap.Web.Utils;
    using Ext = Coolite.Ext.Web;

    /// <summary>
    /// 
    /// </summary>
    public partial class CustomerHistory : BasePage<CustomerHistoryViewModel>, IPermittedPage
    {
        #region QueryString Properties
        /// <summary>
        /// Gets the current customer ID.
        /// </summary>
        public string CurrentCustomerID
        {
            get
            {
                return Request.QueryString["CustomerID"];
            }
        }

        /// <summary>
        /// Gets the name of the current customer.
        /// </summary>
        /// <value>
        /// The name of the current customer.
        /// </value>
        public string CurrentCustomerName
        {
            get
            {
                return Request.QueryString["CustomerName"];
            }
        }
        #endregion

        #region Event Handlers
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Ext.Ext.IsAjaxRequest)
            {
                if (!CurrentCustomerID.IsNullOrEmpty())
                {
                    ICustomer cust = this.ViewModel.GetCustomerByID(CurrentCustomerID);
                    lblName.Text = cust.CustomerName;
                    lblSSID.Text = cust.SSN;

                    this.storeHistory.DataSource = this.ViewModel.GetCustomerLoanHistoryByID(CurrentCustomerID);
                    this.storeHistory.DataBind();
                }
                else if (!CurrentCustomerName.IsNullOrEmpty())
                {
                    lblName.Text = CurrentCustomerName;

                    this.storeHistory.DataSource = this.ViewModel.GetCustomerLoanHistoryByName(CurrentCustomerName);
                    this.storeHistory.DataBind();
                }
            }
        }


        /// <summary>
        /// Handles the Clicked event of the btnBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void btnBack_Clicked(object sender, AjaxEventArgs args)
        {
            this.RedirectToPreviousPage();
        }

        /// <summary>
        /// Handles the SelectedChanged event of the gridLoanHistory control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void gridLoanHistory_SelectedChanged(object sender, AjaxEventArgs e)
        {
            RowSelectionModel rowSelection = gridLoanHistory.SelectionModel.Primary as RowSelectionModel;
            string temp = rowSelection.SelectedRecordID;
            if (!temp.IsNullOrEmpty())
                txtNotes.Text = this.ViewModel.GetLoanNotes(temp.ToGuid());
        }
        #endregion

        #region IPermittedPage Members

        /// <summary>
        /// Gets the name of the permission.
        /// </summary>
        /// <value>
        /// The name of the permission.
        /// </value>
        public string PermissionName
        {
            get { return "CustomerHistory"; }
        }

        #endregion
    }
}
